<!--suppress ALL -->
<template>
	<view>
		<!-- <view class="status_bar">
			<view class="top_view"></view>
    </view>-->
		<!-- <view style="height: 80upx;">
			<view class="tab-bar">
				<view style="width: 120upx; height: 60upx;position: absolute;text-align: center;top: 16upx;">
					<image :src="logo" mode="aspectFill" style="width: 50upx; height: 50upx;border-radius: 50%;"/>
				</view>
				<view style="flex: 1;text-align: center;">{{title}}</view>
				<view style="width: 120upx; height: 60upx;position: absolute;text-align: center;top: 14upx;right: 10upx;" @click="scan">
					<image src="../../static/icon/icon-scan.png" mode="aspectFill" style="width: 40upx; height: 40upx;"/>
				</view>
			</view>
    </view>-->
		<swiper
			:indicator-dots="indicatorDots"
			:autoplay="autoplay"
			:interval="interval"
			:duration="duration"
			indicator-color="#CCCCCC"
			circular="true"
			indicator-active-color="#FFFFFF"
			style="height: 400upx;"
		>
			<swiper-item v-for="(item, index) in bannerList" :key="index"><img class="banner-item-img" :src="item.coverUrl" @click="handleClickAd(item)" /></swiper-item>
		</swiper>

		<view v-if="menu.length > 0">
			<view class="grid-box">
				<view
					v-for="(item, index) in menu"
					:key="index"
					class="grid-options"
					:style="
						index == 0
							? 'border-right: 1px solid #EFEFEF;'
							: index == 1
							? ''
							: index == 2
							? 'border-left:1px solid #EFEFEF;'
							: index % 3 == 0
							? 'border-top:1px solid #EFEFEF;border-right: 1px solid #EFEFEF'
							: index % 3 == 1
							? menu.length == index + 1 || menu.length == index + 2 || menu.length > index + 3
								? 'border-top:1px solid #EFEFEF;'
								: 'border-top:1px solid #EFEFEF;border-bottom:1px solid #EFEFEF;'
							: index % 3 == 2
							? menu.length % 3 == 0
								? 'border-top:1px solid #EFEFEF;border-left: 1px solid #EFEFEF;'
								: 'border-top:1px solid #EFEFEF;border-left: 1px solid #EFEFEF;border-bottom:1px solid #EFEFEF;'
							: ''
					"
					@click="onClick(item)"
				>
					<view v-if="item.modulePath == '/pages/notice/notice' && parseInt(noReadCount) > 0" class="badge" style="background: #F04D44;">{{ noReadCount }}</view>
					<!-- <view v-else class="badge"></view> -->
					<image :src="item.moduleImageUrl" style="width: 100upx;height: 100upx;border-radius: 50%;" />
					<view style="margin-top: 10upx;">{{ item.moduleName }}</view>
				</view>
			</view>
		</view>
		<view style="margin-top: 20upx;">
			<view class="cate-box">
				<view class="cate-title" :class="idx == 1 ? 'cate-active' : ''" style="border-radius: 30upx 0 0 30upx;" @tap="changeCate(1)">新闻动态</view>
				<view class="cate-title" :class="idx == 2 ? 'cate-active' : ''" style="border-radius: 0 30upx 30upx 0;" @tap="changeCate(2)">党建专区</view>
			</view>
			<view class="news-box">
				<news-item v-for="(item, index) in newsList" :key="index" :news="item" />
				<uni-load-more :status="loadingType" :contentText="contentText"></uni-load-more>
			</view>
		</view>

		<model v-if="hidden">
			<view class="model-content">
				<view>{{ msg }}</view>
			</view>
			<view class="model-footer"><button type="primary" class="model-footer-btn primary" @click="hidden = false">确定</button></view>
		</model>
		<download-apk ref="downloadApk" v-if="download" />
	</view>
</template>

<script>
import { post_md5, request_integral } from './../../common/request';
import { setNavTitle, showModal } from '../../common/common';
import NewsItem from '../news/news-item';
import downloadApk from '../../components/download_apk.vue';

export default {
	components: {
		NewsItem,
		downloadApk
	},
	data() {
		return {
			download: false,
			isFirstLoad: true,
			view: null,
			indicatorDots: false,
			autoplay: true,
			interval: 5000,
			duration: 1000,
			bannerList: [],
			newsList: [],
			idx: 1,
			page: 1,
			menu: [],
			loadingType: 'more', //定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
			contentText: {
				contentdown: '上拉加载更多',
				contentrefresh: '正在加载...',
				contentnomore: '没有更多数据了'
			},
			noReadCount: 0,
			hidden: false,
			msg: '',
			title: '',
			logo: '',
			args: null
		};
	},
	onLoad() {
		let updata = uni.getStorageSync('updata') || '';
		if (updata == '') {
			let _this = this;
			if (uni.getSystemInfoSync().platform == 'android') {
				plus.runtime.getProperty(plus.runtime.appid, e => {
					var xhr_svn = new plus.net.XMLHttpRequest();
					xhr_svn.onreadystatechange = function() {
						if (xhr_svn.readyState == 4) {
							if (xhr_svn.status == 200) {
								var res = JSON.parse(xhr_svn.responseText);
								if (res.version != e.version) {
									// if (res.version != 1) {
									showModal(res, _this);
								}
							}
						}
						xhr_svn.open('GET', 'http://app.mkkxy.com/version.json'); //这里的地址是上面json文件的地址
						xhr_svn.send();
					};
				});
			}
			this.loadingText = '上拉加载更多';
			this.loadingType = 'more';
			this.getInfo();
		}
	},
	onShow() {
		let oldDate = uni.getStorageSync('TimeDay');
		let date = new Date();
		let newDate = `${date.getFullYear()}_${date.getMonth()}_${date.getDate()}`;
		if (oldDate) {
			if (oldDate != newDate) {
				request_integral('login', 0);
				uni.setStorageSync('TimeDay', newDate);
			}
		} else {
			request_integral('login', 0);
			uni.setStorageSync('TimeDay', newDate);
		}
		this.getTitleLogo();
		if (!this.isFirstLoad) {
			this.reloadNoReadCount();
		}
		this.isFirstLoad = false;
	},
	// onHide() {
	// 	if (this.view !== null && this.view !== undefined) {
	// 		this.view.close();
	// 		this.view = null;
	// 	}
	// },
	// onUnload() {
	// 	if (this.view !== null && this.view !== undefined) {
	// 		this.view.close();
	// 		this.view = null;
	// 	}
	// },
	onPullDownRefresh() {
		this.getTitleLogo();
		//下拉刷新的时候请求一次数据
		this.page = 1;
		this.idx = 1;
		this.loadingType = 'more';
		this.newsList = [];
		this.getInfo();
	},
	onReachBottom() {
		this.page = this.page == 1 ? 2 : this.page;
		this.getNewsList(this.idx);
	},
	onReachBottom: function() {
		this.page = this.page == 1 ? 2 : this.page;
		this.getNewsList(this.idx);
	},
	methods: {
		getTitleLogo() {
			// if (this.view !== null && this.view !== undefined) {
			// 	this.view.close();
			// 	this.view = null;
			// }
			// #ifdef APP-PLUS
			post_md5('/sys/tenantsysconfig/appinfo').then(res => {
				let data = JSON.parse(res.data);
				setNavTitle(data.hometitle);
				// this.view = setNavLogo(data.logoUrl);
				// this.view.show();
			});
			// #endif
		},
		getInfo() {
			post_md5('/news-app/index', {
				userId: uni.getStorageSync('userId')
			}).then(res => {
				this.bannerList = res.data.bannerList;
				this.menu = res.data.indexCateList;
				this.newsList = res.data.page.list;
				this.noReadCount = res.data.noReadCount;
				uni.stopPullDownRefresh();
			});
		},
		reloadNoReadCount() {
			post_md5('/news-app/notice/noreadcount', {
				userId: uni.getStorageSync('userId')
			}).then(res => {
				this.noReadCount = res.data;
			});
		},
		onClick(item) {
			if (item.modulePath == '/pages/news/news' || item.modulePath == '/pages/course/course') {
				uni.switchTab({
					url: item.modulePath
				});
				uni.setStorageSync('notice', 1);
			} else if (item.modulePath == '/pages/exam/exam-list?examType=4') {
				post_md5('/exam-app/daily/answer/list', {
					userId: uni.getStorageSync('userId')
				}).then(res => {
					let data = res.data || [];
					if (data.length > 0 && data.length === 1) {
						post_md5('/exam-app/daily/answer/record', {
							dailyId: data[0].id,
							userId: uni.getStorageSync('userId')
						}).then(res => {
							if (res.data) {
								uni.showToast({
									title: '今日已完成，请明日再来',
									icon: 'none'
								});
							} else {
								uni.navigateTo({
									url: '/pages/topic/daily-topic' + `?examType=4&id=${data[0].id}&length=${data.length}`
								});
							}
						});
					} else {
						uni.redirectTo({
							url: '/pages/exam/exam-list' + `?examType=4` + '&banner=true'
						});
					}
				});
			} else {
				let path = item.modulePath.split('?');
				switch (path[0]) {
					case '/pages/exam/exam-list':
						uni.redirectTo({
							url: item.modulePath + '&banner=true'
						});
						break;
					default:
						uni.navigateTo({
							url: `${item.modulePath}${item.params !== '' ? '?id=' + item.params : ''}`
						});
						break;
				}
			}
		},
		handleClickAd(item) {
			if (item.modulePath.indexOf('?') !== -1) {
				if (item.modulePath == '/pages/news/news' || item.modulePath == '/pages/course/course') {
					uni.switchTab({
						url: `${item.modulePath}${item.params !== '' ? '&id=' + item.params : ''}`
					});
				} else {
					let path = item.modulePath.split('?');
					switch (path[0]) {
						case '/pages/exam/exam-list':
							uni.redirectTo({
								url: item.modulePath + '&banner=true'
							});
							break;
						default:
							uni.navigateTo({
								url: `${item.modulePath}${item.params !== '' ? '&id=' + item.params : ''}`
							});
							break;
					}
				}
			} else {
				if (item.modulePath === '/pages/news/news' || item.modulePath == '/pages/course/course') {
					var url = `${item.modulePath}${item.params !== '' ? '?id=' + item.params : ''}`
					uni.switchTab({
						url: url
					});
				} else {
					let path = item.modulePath.split('?');
					switch (path[0]) {
						case '/pages/exam/exam-list':
							uni.redirectTo({
								url: item.modulePath + '&banner=true'
							});
							break;
						default:
							uni.navigateTo({
								url: `${item.modulePath}${item.params !== '' ? '?id=' + item.params : ''}`
							});
							break;
					}
				}
			}
		},
		// 查看新闻详情
		goNewsDetail(id) {
			uni.navigateTo({
				url: `../news/news-detail?id=${id}&type=${this.idx}`
			});
		},
		changeCate(index) {
			this.idx = index;
			this.page = 1;
			this.newsList = [];
			this.loadingType = 'more';
			this.getNewsList(index);
		},
		// 获取更多新闻动态
		getNewsList(val) {
			if (this.loadingType !== 'more') {
				//loadingType!=0;直接返回
				return false;
			}
			this.loadingType = 'loading';
			uni.showNavigationBarLoading(); //显示加载动画
			const params = {
				page: this.page,
				type: val
			};
			post_md5('/news-app/news/morenews', params).then(res => {
				this.page++;
				if (res.data.list.length > 0) {
					for (let i in res.data.list) {
						this.newsList.push(res.data.list[i]);
					}
					if (this.newsList.length > 5) {
						this.loadingType = 'more';
					} else {
						this.loadingType = 'noMore';
					}
					uni.hideNavigationBarLoading(); //关闭加载动画
				} else {
					this.loadingType = 'noMore';
					uni.hideNavigationBarLoading(); //关闭加载动画
				}
			});
		},

		// 扫码
		scan() {
			var that = this;
			uni.scanCode({
				onlyFromCamera: true,
				success: function(res) {
					const params = {
						userId: uni.getStorageSync('userId'),
						trainId: res.result
					};
					post_md5('/train/trainAttendanceByUser', params).then(res => {
						this.hidden = true;
						if (res.data == null) {
							that.msg = '成功';
						} else {
							that.msg = res.msg;
						}
					});
				}
			});
		}
	}
};
</script>

<style>
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
	background-color: #ffffff;
}

.top_view {
	height: var(--status-bar-height);
	width: 100%;
	background-color: #ffffff;
	position: fixed;
	top: 0;
	z-index: 999;
}

.tab-bar {
	height: 80upx;
	line-height: 80upx;
	color: #000000;
	font-size: 32upx;
	/* font-weight: bold; */
	width: 100%;
	background: #ffffff;
	position: fixed;
	top: var(--status-bar-height);
	z-index: 999;
	/* border: #000000 1px solid; */
}

.banner-item-img {
	width: 750upx;
	height: 400upx;
	/* border-radius: 20upx; */
}

.grid-box {
	width: 666upx;
	background: rgba(255, 255, 255, 1);
	box-shadow: 0px 0px 20px 0px rgba(71, 73, 78, 0.2);
	border-radius: 15px;
	margin: 0 20upx;
	padding: 40upx 22upx;
	display: flex;
	flex-wrap: wrap;
	flex-direction: row;
	position: relative;
	top: -20upx;
	z-index: 100;
}

.grid-options {
	position: relative;
	width: 220upx;
	height: 180upx;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	text-align: center;
	font-size: 28upx;
	color: #666666;
	padding-bottom: 10upx;
}

.badge {
	width: 40upx;
	height: 40upx;
	line-height: 40upx;
	border-radius: 50%;
	position: absolute;
	right: 40upx;
	top: 20upx;
	z-index: 10;
	font-size: 20upx;
	text-align: center;
	color: #fff;
}

.news-box {
	background: #ffffff;
	margin: 10upx 0;
}

.news-item-box {
	border-bottom: 1px solid #eeeeee;
}

.news-cover-img {
	width: 250upx;
	height: 180upx;
	margin-right: 20upx;
}

/*noinspection ALL*/
.news-title {
	flex: 1;
	font-size: 28upx;
	font-weight: bold;
	color: #333333;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 3;
	line-height: 40upx;
	height: 120upx;
	padding-right: 30upx;
}

.news-time {
	font-size: 24upx;
	font-weight: 400;
	color: #888888;
	display: flex;
	align-items: center;
	margin-top: 20upx;
}

.icon {
	width: 28upx;
	height: 26upx;
	margin-right: 10upx;
}
</style>
